package com.ui.zhixiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ui.zhixiao.common.R;
import com.ui.zhixiao.domain.Article;
import com.ui.zhixiao.domain.Category;
import com.ui.zhixiao.dto.ArticleDto;
import com.ui.zhixiao.service.ArticleService;
import com.ui.zhixiao.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章
     *
     * @param article
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Article article) {
        articleService.save(article);
        return R.success("新增文章成功");
    }


    /**
     * 文章信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page<Article> pageInfo = new Page<>(page,pageSize);
        Page<ArticleDto> articleDtoPage = new Page<>();

        //条件构造
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();

        //添加条件
        queryWrapper.like(name!=null,Article::getTitle,name);

        //排序
        queryWrapper.orderByDesc(Article::getUpdateTime);

        //执行
        articleService.page(pageInfo);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,articleDtoPage,"records");

        List<Article> records = pageInfo.getRecords();
        List<ArticleDto> list = records.stream().map((item)->{
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(item,articleDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!= null){
                String categoryName = category.getName();
                articleDto.setCategoryName(categoryName);
            }
            return articleDto;
        }).collect(Collectors.toList());

        articleDtoPage.setRecords(list);
        return R.success(articleDtoPage);

    }


    /**
     * 根据id查文章信息 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Article> get(@PathVariable Long id){
        Article article = articleService.getById(id);
        if(article!=null){
            return R.success(article);
        }
        return R.error("没有查询到文章信息");
    }

    /**
     * 根据id修改文章信息
     * @param article
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Article article){
        articleService.updateById(article);
        return R.success("修改文章信息成功");
    }


    /**
     * 批量启用 停止
     * @param st
     * @param ids
     * @return
     */
    @PostMapping("/status/{st}")
    public R<String> setStatus(@PathVariable int st,String ids){
        //处理ids
        String[] split = ids.split(",");
        List<Long> idList = Arrays.stream(split).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());

        //重新设置状态
        List<Article> articles = idList.stream().map((item)->{
            Article article = new Article();
            article.setId(item);
            article.setStatus(st);
            return article;
        }).collect(Collectors.toList());

        articleService.updateBatchById(articles);
        return R.success("批量操作成功");
    }

    /**
     * 删除 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        String[] split = ids.split(",");
        List<Long> idList = Arrays.stream(split).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
        articleService.removeByIds(idList);
        return R.success("删除成功");
    }

}
